package com.example.myapplication.slice;

import com.alibaba.fastjson.JSON;
import com.example.myapplication.RegisterAbility;
import com.example.myapplication.ResourceTable;
import com.example.myapplication.entity.TUser;
import com.example.myapplication.utl.ProgressDialogUtil;
import com.example.myapplication.utl.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;
import com.example.myapplication.utl.*;

public class RegisterAbilitySlice extends AbilitySlice {
    TextField tf_name;
    TextField tf_password;
    TextField tf_password2;
    Button btn_register;
    ProgressDialogUtil progressDialogUtil=new ProgressDialogUtil(this);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_register);

        tf_name=(TextField)findComponentById(ResourceTable.Id_rg_name);
        tf_password=(TextField)findComponentById(ResourceTable.Id_rg_password);
        tf_password2=(TextField)findComponentById(ResourceTable.Id_rg_password2);
        btn_register=(Button)findComponentById(ResourceTable.Id_rg_rgs);
        findComponentById(ResourceTable.Id_rg_rt).setClickedListener(component->{
            terminateAbility();
        });
        btn_register.setClickedListener(component -> register());
    }
    public void register(){
        String name = tf_name.getText();
        if (name == null || name.trim().length() == 0) {
            ToastUtil.makeToast(getContext(), "帐号不能为空", ToastUtil.TOAST_LONG);
            return;
        }
        String password = tf_password.getText();
        String password2 = tf_password2.getText();
        if (password == null) {
            ToastUtil.makeToast(getContext(), "密码不能为空", ToastUtil.TOAST_LONG);
            return;
        }
        if (!password.equals(password2)) {
            ToastUtil.makeToast(getContext(), "密码不一样", ToastUtil.TOAST_LONG);
            return;
        }
        progressDialogUtil.showProgress(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                String res = HttpClientUtil.doGet(ContainUtil.REGISTER_USER_URL + "?name=" + name + "&password=" + password);
                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        progressDialogUtil.showProgress(false);
                        TUser user = null;
                        try {
                            user = JSON.parseObject(res, TUser.class);
                        } catch (Exception e) {
                        }
                        if (user != null) {
                            ToastUtil.makeToast(getContext(), "注册成功", ToastUtil.TOAST_LONG);
                            terminateAbility();
                            return;
                        } else {
                            ToastUtil.makeToast(getContext(), "注册失败", ToastUtil.TOAST_LONG);
                        }
                    }
                });
            }
        }).start();
    }




    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


}
